package com.food.dao;

import com.food.model.Order;
import com.food.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDAO {
    public List<Order> getOrders(String search, int page, int pageSize) throws SQLException {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM orders WHERE username LIKE ? OR menuname LIKE ? LIMIT ?, ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, "%" + search + "%");
            stmt.setString(2, "%" + search + "%");
            stmt.setInt(3, (page - 1) * pageSize);
            stmt.setInt(4, pageSize);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setUserid(rs.getInt("userid"));
                order.setUsername(rs.getString("username"));
                order.setMenuid(rs.getInt("menuid"));
                order.setMenuname(rs.getString("menuname"));
                order.setCount(rs.getInt("count"));
                order.setAmount(rs.getString("amount"));
                order.setStatus(rs.getInt("status"));
                order.setCretime(rs.getString("cretime"));
                orders.add(order);
            }
        }
        return orders;
    }

    public int getTotalCount(String search) throws SQLException {
        String sql = "SELECT COUNT(*) FROM orders WHERE username LIKE ? OR menuname LIKE ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, "%" + search + "%");
            stmt.setString(2, "%" + search + "%");
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
        }
        return 0;
    }

    public void addOrder(Order order) throws SQLException {
        String sql = "INSERT INTO orders (userid, username, menuid, menuname, count, amount, status) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, order.getUserid());
            stmt.setString(2, order.getUsername());
            stmt.setInt(3, order.getMenuid());
            stmt.setString(4, order.getMenuname());
            stmt.setInt(5, order.getCount());
            stmt.setString(6, order.getAmount());
            stmt.setInt(7, order.getStatus());
            stmt.executeUpdate();
        }
    }

    public void updateOrder(Order order) throws SQLException {
        String sql = "UPDATE orders SET userid = ?, username = ?, menuid = ?, menuname = ?, count = ?, amount = ?, status = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, order.getUserid());
            stmt.setString(2, order.getUsername());
            stmt.setInt(3, order.getMenuid());
            stmt.setString(4, order.getMenuname());
            stmt.setInt(5, order.getCount());
            stmt.setString(6, order.getAmount());
            stmt.setInt(7, order.getStatus());
            stmt.setInt(8, order.getId());
            stmt.executeUpdate();
        }
    }

    public void deleteOrder(int id) throws SQLException {
        String sql = "DELETE FROM orders WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    public Order getOrderById(int id) throws SQLException {
        String sql = "SELECT * FROM orders WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setUserid(rs.getInt("userid"));
                order.setUsername(rs.getString("username"));
                order.setMenuid(rs.getInt("menuid"));
                order.setMenuname(rs.getString("menuname"));
                order.setCount(rs.getInt("count"));
                order.setAmount(rs.getString("amount"));
                order.setStatus(rs.getInt("status"));
                order.setCretime(rs.getString("cretime"));
                return order;
            }
        }
        return null;
    }

    public void updateOrderStatus(int id, int status) throws SQLException {
        String sql = "UPDATE orders SET status = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, status);
            stmt.setInt(2, id);
            stmt.executeUpdate();
        }
    }

}
